home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 June / ccd0605.iso / Software / Freeware / Programare / highlight / highlight-W32GUI-2.2-10b-Setup.exe / {app} / src / codegenerator.h < prev    next >
C/C++ Source or Header  |  2005-03-31  |  14KB  |  461 lines

  1. /***************************************************************************
  2.                           codeparser.h  -  description
  3.                              -------------------
  4.     begin                : Die Jul 9 2002
  5.     copyright            : (C) 2002 by Andre Simon
  6.     email                : andre.simon1@gmx.de
  7.  ***************************************************************************/
  8.  
  9. /***************************************************************************
  10.  *                                                                         *
  11.  *   This program is free software; you can redistribute it and/or modify  *
  12.  *   it under the terms of the GNU General Public License as published by  *
  13.  *   the Free Software Foundation; either version 2 of the License, or     *
  14.  *   (at your option) any later version.                                   *
  15.  *                                                                         *
  16.  ***************************************************************************/
  17.  
  18. #ifndef CODEPARSER_H
  19. #define CODEPARSER_H
  20.  
  21. #include <iostream>
  22. #include <sstream>
  23. #include <string>
  24. #include <iomanip>
  25. #include <cctype>
  26.  
  27. #include "languagedefinition.h"
  28. #include "documentstyle.h"
  29. #include "ASFormatter.h"
  30. #include "preformatter.h"
  31. #include "enums.h"
  32.  
  33.  
  34. #define NUMBER_BUILTIN_STYLES 10
  35. #define LINE_NUMBER_WIDTH     5
  36. #define MAX_LINE__WIDTH       80
  37.  
  38. #define OUTPUT_FLAG_LN    1
  39. #define OUTPUT_FLAG_LNZ   2
  40. #define OUTPUT_FLAG_FRAG  4
  41.  
  42. /** The highlight namespace contains all classes and data structures
  43.     needed for parsing input data.
  44. */
  45. namespace highlight {
  46.  
  47. /** \brief Base class for parsing. Works similar to a Turing machine.
  48.  
  49.     The virtual class provides source code parsing functioality, based on
  50.     information stored in language definitions.<br>
  51.     Deriving classes have to define the output format.<br>
  52.     Codegenerator is a singleton class.
  53.  
  54. * @author Andre Simon
  55. */
  56.  
  57. class CodeGenerator
  58.   {
  59.  
  60.   public:
  61.  
  62.     virtual ~CodeGenerator();
  63.  
  64.     /**
  65.       Get appropriate Codegenerator instance
  66.       \param type Output file type (HTML, XHTML, RTF, LATEX, TEX, XSLFO, ANSI)
  67.       \param styleInfoPath Path to formatting style information
  68.       \param styleInPath Path to style definition input file (to be included in styleOutPath)
  69.       \param styleOutPath Path to style definition output file (CSS path for HTML output)
  70.       \param encoding Output file encoding name
  71.       \param includeStyle Switch to include style information in output file (only XHTML, HTML)
  72.       \param attachAnchors Switch to attach anchors to line numbers (only XHTML, HTML)
  73.       \param replaceQuotes Switch to replace quotes by \dq{} (only LATEX)
  74.       \param fopCompatible Switch to generate FO for Apache FOP  (only XSLFO)
  75.       \param omitEncoding Switch to omit encoding info in output document
  76.       \param ln Set true if line numbers should be printed
  77.       \param lnz Set true if leading space of line numbers should be filled with 0's
  78.       \param fragment Set true if document header and footer should be omitted
  79.       \param numSpaces Number of spaces which replace a tab
  80.       \param lineWrappingMode Line wrapping mode
  81.     */
  82.     static CodeGenerator* getInstance(OutputType type,
  83.                                       const string& styleInfoPath,
  84.                                       const string& styleInPath,
  85.                                       const string& styleOutPath,
  86.                                       const string& encoding,
  87.                                       bool includeStyle,
  88.                                       bool attachAnchors,
  89.                                       bool replaceQuotes,
  90.                                       bool fopCompatible,
  91.                                       int numSpaces,
  92.                                       WrapMode lineWrappingMode,
  93.                                       bool ln,
  94.                                       bool lnz,
  95.                                       bool fragment,
  96.                                       bool omitEncoding );
  97.  
  98.     /** Deletes the singleton CodeGenerator instance.
  99.         Call this method if getInstance was already called, or if you want to
  100.         free the momory after usage.*/
  101.     static void deleteInstance();
  102.  
  103.     /**
  104.      Generates output
  105.      \param inFileName Path of input file (if empty use stdin)
  106.      \param outFileName Path of input file (if empty use stdout)
  107.  
  108.      \return ParseError
  109.     */
  110.     ParseError printOutput(const string &inFileName, const string &outFileName);
  111.  
  112.     /** \return True if document style was found */
  113.     bool styleFound();
  114.  
  115.     /** \return True if reformatting of current input is disabled */
  116.     bool formattingDisabled();
  117.  
  118.     /** \return True if reformatting of current input is possible */
  119.     bool formattingIsPossible();
  120.  
  121.     /** \param langDefPath Absolute path to language definition
  122.         \return  Failure: LOAD_FAILED; Reload necessary: LOAD_NEW,
  123.                  no reload necessary:  LOAD_NONE */
  124.     LoadResult initLanguage(const string& langDefPath);
  125.  
  126.     /** \return Language definition*/
  127.     LanguageDefinition &getLanguage();
  128.  
  129.     /** tell parser to output line numbers
  130.        \param  flag true if line numbers should be printed
  131.     */
  132.     void setPrintLineNumbers(bool flag);
  133.  
  134.     /** \return line number flag */
  135.     bool getPrintLineNumbers();
  136.  
  137.  
  138.     /** tell parser to output line numbers filled with zeroes
  139.         \param  flag true if zeroes should be printed
  140.     */
  141.     void setPrintZeroes(bool flag);
  142.  
  143.     /** \return print zeroes flag */
  144.     bool getPrintZeroes();
  145.  
  146.     /** tell parser to omit document header and footer
  147.        \param  flag true if output should be fragmented
  148.     */
  149.     void setFragmentCode(bool flag);
  150.  
  151.     /** \return fragment flag */
  152.     bool getFragmentCode();
  153.  
  154.     /** tell parser the style name
  155.        \param s path to style definition
  156.     */
  157.     void setStyleName(const string& s);
  158.  
  159.     /** \return style path */
  160.     const string& getStyleName();
  161.  
  162.     /** tell parser the wrapping mode
  163.        \param lineWrappingStyle wrapping style
  164.        \param lineLength max line length
  165.        \param numberSpaces number of spaces which replace a tab
  166.     */
  167.     void setPreformatting(WrapMode lineWrappingStyle, unsigned int lineLength,int numberSpaces);
  168.  
  169.     /** \return wrapping style */
  170.     WrapMode getLineWrapping();
  171.  
  172.     /** tell parser the include style definition in output
  173.         \param flag true if style should be included
  174.      */
  175.     void setIncludeStyle(bool flag);
  176.  
  177.    /** Print style definitions to external file
  178.      \param outFile Path of external style definition
  179.      */
  180.     bool printExternalStyle(const string &outFile);
  181.  
  182.    /** Print index file with all input file names
  183.       \param fileList List of output file names
  184.       \param outPath Output path
  185.     */
  186.     virtual bool printIndexFile(const vector<string> & fileList,
  187.                                 const string &outPath);
  188.  
  189.    /** initialize source code indentation
  190.        \param indentSchemePath Path of indentation scheme
  191.        \return true id successfull
  192.     */
  193.     bool initIndentationScheme(const string&indentSchemePath);
  194.  
  195.     /** Set style input path
  196.        \param s path to style input file
  197.      */
  198.     void setStyleInputPath(const string& path);
  199.  
  200.     /** Set style output path
  201.       \param s path to style output file
  202.     */
  203.     void setStyleOutputPath(const string& path);
  204.  
  205. /** Set output type
  206.     \param s output type
  207.  */
  208.     void setType(OutputType t);
  209.  
  210.     /**
  211.        \return style input file path
  212.     */
  213.     const string&  getStyleInputPath();
  214.  
  215.     /**
  216.        \return style output file path
  217.      */
  218.     const string&  getStyleOutputPath();
  219.  
  220. protected:
  221.  
  222.     CodeGenerator();
  223.  
  224.     //! CodeGenerator Constructor
  225.     /**
  226.         \param colourTheme Name of coloring style being used
  227.     */
  228.     CodeGenerator(const string &colourTheme);
  229.  
  230.     /** \param c Character to be masked
  231.         \return Escape sequence of output format */
  232.     virtual string maskCharacter(unsigned char c) = 0;
  233.  
  234.     /** \param s string
  235.        \return Copy of s with all escaped characters */
  236.     string maskString(const string &s ) ;
  237.  
  238.     /** \param s Symbol string
  239.         \param searchPos Position where search starts
  240.         \return Found state (integer value)  */
  241.     State getState(const string &s, unsigned int searchPos);
  242.  
  243.     /** \return Next identifier in current line of code */
  244.     string getIdentifier();
  245.  
  246.     /** \return Next number in current line of code */
  247.     string getNumber();
  248.  
  249.     /** Insert line number at the beginning of current output line */
  250.     virtual void insertLineNumber(bool insertNewLine=true);
  251.  
  252.     /** Prints document footer*/
  253.     virtual string getFooter() = 0;
  254.  
  255.     /** Prints document body*/
  256.     virtual void printBody() = 0;
  257.  
  258.     /** prints document header
  259.        \param  title Title of the document
  260.     */
  261.     virtual string getHeader(const string &title) = 0;
  262.  
  263.     /** Get current line number
  264.       \return line number  */
  265.     unsigned int getLineNumber();
  266.  
  267.  
  268.     /** Tag Delimiters for every colour style*/
  269.     vector <string> styleTagOpen, styleTagClose;
  270.  
  271.     /** Description of document colour style*/
  272.     DocumentStyle docStyle;
  273.  
  274.     /** Language definition*/
  275.     LanguageDefinition langInfo;
  276.  
  277.     /** Tag for inserting line feeds*/
  278.     string newLineTag;
  279.  
  280.     /** String that represents a white space in output */
  281.     string spacer;
  282.  
  283.     /** file input*/
  284.     istream *in;
  285.  
  286.     /** file output*/
  287.     ostream *out;
  288.  
  289.     /** Tags which enclose white space indentation blocks */
  290.     string maskWsBegin, maskWsEnd;
  291.  
  292.     /** Style comment delimiters */
  293.     string styleCommentOpen, styleCommentClose;
  294.  
  295.     /** Test if maskWsBegin and maskWsEnd should be applied */
  296.     bool maskWs;
  297.  
  298.     /** Test if whitespace sould always be separated from enclosing tokens */
  299.     bool excludeWs;
  300.  
  301.     /** Test if header and footer should be omitted */
  302.     bool fragmentOutput;
  303.  
  304.     /** Test if line numbers should be printed */
  305.     bool showLineNumbers;
  306.  
  307.     /** Test if leading spyce of line number should be filled with zeroes*/
  308.     bool lineNumberFillZeroes;
  309.  
  310.     /** Current line of input file*/
  311.     string line;
  312.  
  313.     /** Current line number */
  314.     unsigned int lineNumber;
  315.  
  316.     // Zeigt den aktuellen Zustand an
  317.     // wird nicht in getCurrentState gesetzt, da nur Zust├ñnde interessant
  318.     // sind, die als Index auf die styleCloseTags und styleOpenTags verwendet
  319.     // werden k÷nnen
  320.     /** Current state*/
  321.     State currentState;
  322.  
  323.     /** keyword class id, used to apply the corresponding keyword style*/
  324.     unsigned int currentKeywordClass;
  325.  
  326.     /** Processes origin state */
  327.     void processRootState();
  328.  
  329.     /** return line break sequence */
  330.     virtual string getNewLine();
  331.  
  332.     /**
  333.        \param s current state
  334.        \return Index of style tag corresponding to the states
  335.     */
  336.     unsigned int getStyleID(State s, unsigned int kwClassID = 0);
  337.  
  338.     /** \return line index */
  339.     unsigned int  getLineIndex();
  340.  
  341.     /** print all remaining white space*/
  342.     void flushWs();
  343.  
  344.     /**
  345.     \return Content of user defined input style
  346.     */
  347.     string readUserStyleDef();
  348.  
  349.     /**
  350.      \return Style definition of the chosen output format
  351.     */
  352.     virtual string  getStyleDefinition() {return "";};
  353.  
  354.     /** contains white space, which will be printed after a closing tag */
  355.     string wsBuffer;
  356.  
  357.     /**
  358.        Flag to test if style definition should be included in output document
  359.      */
  360.     bool includeStyleDef;
  361.  
  362. private:
  363.  
  364.     CodeGenerator(const CodeGenerator&){}
  365.     CodeGenerator& operator=(CodeGenerator&){ return *this;}
  366.  
  367.     static CodeGenerator* generator;
  368.  
  369.     /** return matching open and close tags of the given state */
  370.     virtual string getMatchingOpenTag(unsigned int) = 0;
  371.     virtual string getMatchingCloseTag(unsigned int) = 0;
  372.  
  373.    /** open a new tag, set current state to s*/
  374.     void openTag(State s);
  375.  
  376.     /** close opened tag, clear current state */
  377.     void closeTag(State s);
  378.  
  379.     void closeTag(unsigned int styleID);
  380.  
  381.     void openTag(unsigned int styleID);
  382.  
  383.     // path to style definition file
  384.     string  stylePath;
  385.  
  386.     // contains current position in line
  387.     unsigned int lineIndex;
  388.  
  389.     /**last character of the last line*/
  390.     unsigned char terminatingChar;
  391.  
  392.     /** Class for reformatting */
  393.     astyle::ASFormatter *formatter;
  394.  
  395.     /** Class for line wrapping and tab replacement*/
  396.     PreFormatter *preFormatter;
  397.  
  398.     /** Flag to test if formatting is enabled with current input document*/
  399.     bool formattingEnabled;
  400.  
  401.  
  402.     /** Flag to test if formatting is possible with current input document*/
  403.     bool formattingPossible;
  404.  
  405.     /** contains the current token*/
  406.     string token;
  407.  
  408.     string styleInputPath, styleOutputPath;
  409.  
  410.     /** Resets parser to origin state, call this after every file conversion */
  411.     void reset();
  412.  
  413.     /** read new line from in stream */
  414.     bool readNewLine(string &newLine);
  415.  
  416.     /** return next character from in stream */
  417.     unsigned char getInputChar();
  418.  
  419.     OutputType outputType;
  420.  
  421.    /** return new state */
  422.     State getCurrentState ( bool lastStateWasNumber=false);
  423.  
  424.     /** Methods that represent a parsing state */
  425.     bool processKeywordState(State myState) ;
  426.     bool processNumberState() ;
  427.     bool processMultiLineCommentState();
  428.     bool processSingleLineCommentState();
  429.     bool processStringState(State oldState);
  430.     bool processEscapeCharState();
  431.     bool processDirectiveState();
  432.     bool processTagState();
  433.     bool processSymbolState();
  434.     void processWsState();
  435.  
  436.     /** gibt true zurck, falls c ein erlaubter Character innerhalb von Keyword
  437.         oder Typbezeichner ist */
  438.     bool isAllowedChar(char c) ;
  439.  
  440.     /** returns true if curret token is the first in line and no whitespace */
  441.     bool isFirstNonWsChar() ;
  442.  
  443.     /** print escaped token and clears it */
  444.     void printMaskedToken(bool flushWhiteSpace=true);
  445.  
  446.     /** print escape sequence */
  447.     void skipEscapeSequence();
  448.  
  449.     void closeKWTag(unsigned int styleID);
  450.     void openKWTag(unsigned int styleID);
  451.  
  452.     /** look for special commands in comments, take action in derived class
  453.         \return true if command was found
  454.     */
  455.     bool checkSpecialCmd();
  456.  
  457.   };
  458. }
  459.  
  460. #endif
  461.